package com.bbcare.oms.plat.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.setting.dialect.Props;
import cn.hutool.setting.dialect.PropsUtil;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.HttpUtils;
import com.bbcare.oms.plat.service.IGuianSSOService;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class GuianSSOService implements IGuianSSOService {


    /**
     * 验证token自动登录转到项目首页
     * 相应的数据结构
     * {
     * // 获取会话标示键值，客户端默认获取到的就是这个Key "Key":"9749e881-dfcb-405e-9d4d-30ade2326428",
     * // 是否已经过期
     * "IsExpired":false,
     * // 获取会话的注册时间，即开始时间
     * "RegistryTime":"0001-01-01T00:00:00",
     * // 获取或设置会话的最后活动时间
     * "LastActiveTime":"2017-05-22T15:14:40.7758469+08:00",
     * // 是否为永久标示
     * "IsPersistent":false,
     * // 过期时间
     * "ExpireSlide":5,
     * // 用户的登录IP
     * "IP":null,
     * // 用户的登录密码(可配置为不显示)
     * "PassWord":"123456",
     * // 用户ID
     * "Identity":"test",
     * // 用户账号
     * "UserCode":"test",
     * // 用户姓名
     *             "UserName":"测试用户"
     * }
     */
    @Override
    public String handleTokenToIndex(String token, String authkey, String authEndTime, String transactionId, HttpServletRequest request) throws Exception {
        System.out.println("贵安SSO============");
        System.out.println("token: "+token);
        System.out.println("authkey: "+authkey);
        System.out.println("authEndTime: "+authEndTime);
        System.out.println("transactionId: "+transactionId);
        final Base64 base64 = new Base64();
        if(StringUtils.isNotBlank(authkey)){
            authkey = base64.encodeToString(authkey.getBytes("UTF-8"));
            System.out.println("解码authkey: "+authkey);
        }
        if(StringUtils.isNotBlank(authEndTime)){
            authEndTime = base64.encodeToString(authEndTime.getBytes("UTF-8"));
            System.out.println("解码authEndTime: "+authEndTime);
        }
        if(StringUtils.isNotBlank(transactionId)){
            transactionId = base64.encodeToString(transactionId.getBytes("UTF-8"));
            System.out.println("解码transactionId: "+transactionId);
        }
        //调用集成厂商的接口验证token返回信息
        String param = "token="+token;
        String response = HttpUtils.sendGet("http://172.30.11.5:8001/api/tokenapi/GetUser", param);
        System.out.println("返回信息:"+JSONObject.parseObject(response));
        JSONObject jsonobject = JSONObject.parseObject(response);
        //是否已经过期
        if(jsonobject.getBooleanValue("IsExpired")){
            throw new Exception("token已过期,跳转登录失败！");
        }
        String userCode = jsonobject.getString("UserCode");
        /*String userName = jsonobject.getString("UserCode");
        String identity = jsonobject.getString("Identity");
        String passWord = jsonobject.getString("PassWord");*/
        //调用映射文件
        Props props = PropsUtil.get("account.properties");
        return props.getStr(userCode,"");
    }
}
